package com.djhu.hadoop.fileservice;
/**
 * Created by zw on 2017/12/15.
 */

import com.djhu.hadoop.BasicFileInfo;
import com.djhu.hadoop.xmlreader.FormatXmlReader;
import com.djhu.hadoop.xmlreader.XmlReader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
public class LocalFileService implements FileService{
    private static final Log logger = LogFactory.getLog(LocalFileService.class);

    private XmlReader xmlReader;

    @Override
    public BasicFileInfo upload(File file, String remote) {
        String xml = xmlReader.readXml(file);
        xml = xml.concat(System.getProperty("line.separator"));
        BasicFileInfo basicFileInfo = upload(xml, remote);
        basicFileInfo.setLocalFile(file.getAbsolutePath());
        return basicFileInfo;
    }

    @Override
    public BasicFileInfo upload(byte[] bytes, String remote) {
        RandomAccessFile randomAccessFile = null;
        BasicFileInfo basicFileInfo = new BasicFileInfo();
        try {
            File remoteFile = new File(remote);
            randomAccessFile = new RandomAccessFile(remote,"rw");
            long start =randomAccessFile.length();
            basicFileInfo.setRemoteFile(remote);
            basicFileInfo.setStart(start);
            basicFileInfo.setLength(bytes.length);
            randomAccessFile.seek(start);
            randomAccessFile.write(bytes);
        } catch (Exception e) {
            logger.error("upload file error "+ remote ,e);
            throw new RuntimeException(e);
        } finally {
            if (randomAccessFile != null){
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return basicFileInfo;
    }

    @Override
    public byte[] readBytes(BasicFileInfo basicFileInfo) {
        RandomAccessFile randomAccessFile = null;
        RandomAccessFile fileWriter = null;
        try {
            randomAccessFile = new RandomAccessFile(basicFileInfo.getRemoteFile(),"rw");
            byte[] bytes = new byte[(int) basicFileInfo.getLength()];
            randomAccessFile.seek((int) basicFileInfo.getStart());
            randomAccessFile.readFully(bytes);
            return  bytes;
        } catch (IOException e) {
            logger.error("error happened while read " +basicFileInfo.toString(),e);
            throw new RuntimeException(e);
        }finally {
            if (randomAccessFile != null){
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public BasicFileInfo upload(String file, String remote) {
        try {
            return upload(file.getBytes("UTF-8"),remote);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public BasicFileInfo doanload(BasicFileInfo basicFileInfo)  {
        RandomAccessFile fileWriter = null;
        try {
            byte[] bytes = readBytes(basicFileInfo);
            fileWriter = new RandomAccessFile(basicFileInfo.getLocalFile(),"rw");
            fileWriter.write(bytes);
        } catch (IOException e) {
            logger.error("error happened while read " +basicFileInfo.toString(),e);
            throw new RuntimeException(e);
        }finally {
            if (fileWriter != null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return basicFileInfo;
    }

    public XmlReader getXmlReader() {
        return xmlReader;
    }

    public void setXmlReader(XmlReader xmlReader) {
        this.xmlReader = xmlReader;
    }

    public static void main(String[] args) {
        LocalFileService localFileService = new LocalFileService();
        FormatXmlReader formatXmlReader = new FormatXmlReader();
        localFileService.setXmlReader(formatXmlReader);
        File file = new File("./1.2.3.4.111.111.1.xml");
        System.out.println(localFileService.upload(file,"./test.txt"));
        System.out.println(localFileService.upload("helloworld","./test.txt"));
        System.out.println(localFileService.upload("我是hahaman\n","./test.txt"));
        System.out.println(localFileService.upload("我是测试换行的————","./test.txt"));
        localFileService.upload("dsds\n","./test.txt");
        localFileService.upload("dsds\n","./test.txt");//76 //length=9058,

        BasicFileInfo basicFileInfo = new BasicFileInfo();
        basicFileInfo.setStart(27317);
        basicFileInfo.setLength(9058);
        basicFileInfo.setRemoteFile("./test.txt");
        basicFileInfo.setLocalFile("./dst.txt");
        basicFileInfo = localFileService.doanload(basicFileInfo);
        System.out.println(basicFileInfo);
    }
}